
from datetime import datetime
from typing import Union, Any, Optional, List

from pydantic import BaseModel, EmailStr, Field
from sqlalchemy import BigInteger, Boolean, Column, DateTime, ForeignKey, Integer, \
    String, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from sqlalchemy.schema import Sequence

from app.common.database import Base
from app.models.base import TableBase, TableBase2


class Account(TableBase2):
    """用户账户表"""
    __tablename__ = "t_account"

    account_no = Column(String(32), primary_key=True, unique=True, index=True, comment='账户编号')
    user_name = Column(String(24), nullable=False, comment='用户账号')
    user_type = Column(Integer, nullable=False, comment='用户类型')
    currency = Column(String(8), nullable=False, comment='币种')
    status = Column(Integer, nullable=False, comment='状态')

    __table_args__ = (
        UniqueConstraint('user_name', 'currency'),
    )
    

class SubAccount(TableBase2):
    """科目账户表"""
    __tablename__ = "t_sub_account"

    sub_acc_no = Column(Integer, primary_key=True, index=True)
    primary_acc_no = Column(String(32), primary_key=True, unique=True, index=True, comment='主账户编号')
    sub_type = Column(Integer, nullable=False, comment='账户类型（1余额账户;2资金冻结;3交易冻结）')
    balance = Column(BigInteger, nullable=False, comment='账户类型（1余额账户;2资金冻结;3交易冻结）')
    balance_sign = Column(String(24), nullable=False, comment='金额签名')
    status = Column(Integer, nullable=False, comment='状态')

    __table_args__ = (
        UniqueConstraint('primary_acc_no', 'sub_type'),
    )
    

class AccountStatement(TableBase2):
    """账户资金流水表"""
    __tablename__ = "t_account_statement"

    id = Column(Integer, primary_key=True, index=True, comment='账户资金流水明细ID')
    stmt_id = Column(String(32), unique=True, index=True, nullable=False, comment='流水ID')
    account_no = Column(String(32), nullable=False, comment='账户编号')
    sub_acc_no = Column(String(20), comment='科目账户编号')
    accounting_type = Column(Integer, nullable=False, comment='记账类型1-交易变更 2-手动调账')
    sub_type = Column(Integer, nullable=False, comment='账户类型 1余额账户;2资金冻结;3交易冻结')
    total_amount = Column(Integer, nullable=False, comment='变动金额')
    trans_amount = Column(Integer, comment='交易金额')
    trans_fee = Column(Integer, comment='交易费用')
    balance = Column(Integer, nullable=False, comment='账户余额')
    order_no = Column(String(32), comment='关联订单编号')


class AccountAdjustDetail(TableBase2):
    """账户调账明细表"""
    __tablename__ = "t_account_adjust_detail"

    id = Column(Integer, primary_key=True, index=True, comment='账户资金流水明细ID')
    stmt_id = Column(String(32), unique=True, index=True, nullable=False, comment='流水ID')
    account_no = Column(String(32), nullable=False, comment='账户编号')
    sub_account_no = Column(String(20), comment='科目账户编号')
    accounting_type = Column(Integer, nullable=False, comment='记账类型1-交易变更 2-手动调账')
    sub_type = Column(Integer, nullable=False, comment='账户类型 1余额账户;2资金冻结;3交易冻结')
    trans_amount = Column(Integer, nullable=False, comment='发生金额')
    trans_fee = Column(Integer, nullable=False, comment='总费用')
    balance = Column(Integer, nullable=False, comment='账户余额')
    order_no = Column(String(32), comment='关联订单编号')

